#include "wxSceneTool.h"
#include "wxSceneView.h"
#include "wxSceneCanvas.h"
wxSceneTool::wxSceneTool(void)
{
}

wxSceneTool::~wxSceneTool(void)
{
}
void wxSceneTool::OnCreate(Runtime *runtime)
{
	pRuntime=runtime;
}
void wxSceneTool::Execute()
{
  pSceneCanvas=pRuntime->GetFocusScene();
  pSceneView=pSceneCanvas->GetView();
  pSceneCanvas->SetCurrentTool(this);

}

void wxSceneTool::OnKeyDown(wxKeyEvent &event)
{
	if (event.GetEventType() == wxEVT_KEY_DOWN)
		pSceneView->getEventQueue()->keyPress(event.GetKeyCode());
	else if (event.GetEventType() == wxEVT_KEY_UP)
		pSceneView->getEventQueue()->keyRelease(event.GetKeyCode());
	pSceneCanvas->Refresh();
}
void wxSceneTool::OnKeyUp(wxKeyEvent &event)
{
	if (event.GetEventType() == wxEVT_KEY_DOWN)
		pSceneView->getEventQueue()->keyPress(event.GetKeyCode());
	else if (event.GetEventType() == wxEVT_KEY_UP)
		pSceneView->getEventQueue()->keyRelease(event.GetKeyCode());
	pSceneCanvas->Refresh();
}

void wxSceneTool::OnMouseWheel(wxMouseEvent &e)
{

}
void wxSceneTool::OnMouse(wxMouseEvent& event)
{
	if (!pSceneView->isValid())
		return;

	if (event.LeftDClick() || event.MiddleDClick() || event.RightDClick())
	{		
		int button = event.GetButton();
		pSceneView->getEventQueue()->mouseDoubleButtonPress(event.GetX(), event.GetY(), button);
		pSceneCanvas->SetFocus();
	}
}
void wxSceneTool::OnMouseCaptureLost(wxMouseCaptureLostEvent& event)
{
	if (pSceneCanvas->GetCapture() == pSceneCanvas)
	{
		wxASSERT(mIsMouseCaptured != 0);
		pSceneCanvas->ReleaseMouse();
		mIsMouseCaptured = 0;
	}
}
void wxSceneTool::OnMouseDown(wxMouseEvent &e)
{
	if(!pSceneView->isValid())
		return;
	int button = e.GetButton();
	pSceneView->getEventQueue()->mouseButtonPress(e.GetX(), e.GetY(), button);
	pSceneCanvas->SetFocus();
	//this->OnMouse(e);
}
void wxSceneTool::OnMouseMove(wxMouseEvent &e)
{
	if(!pSceneView->isValid())
		return;
	if(e.Dragging()){
		if (mIsMouseCaptured == wxMOUSE_BTN_NONE)
		{
			if (e.LeftIsDown())
				mIsMouseCaptured = wxMOUSE_BTN_LEFT;
			else if (e.MiddleIsDown())
				mIsMouseCaptured = wxMOUSE_BTN_MIDDLE;
			else if (e.RightIsDown())
				mIsMouseCaptured = wxMOUSE_BTN_RIGHT;
			else
				assert(false);
			pSceneCanvas->CaptureMouse();
		}
		pSceneCanvas->Refresh();
		pSceneView->getEventQueue()->mouseMotion(e.GetX(), e.GetY());
		pSceneCanvas->SetFocus();
	}

}
void wxSceneTool::OnMouseUp(wxMouseEvent &e)
{
	if(!pSceneView->isValid())
		return;
	int button = e.GetButton();
	if (mIsMouseCaptured == button &&
		pSceneCanvas->GetCapture() == pSceneCanvas)
	{
		pSceneCanvas->ReleaseMouse();
		mIsMouseCaptured = 0;
	}
	pSceneView->getEventQueue()->mouseButtonRelease(e.GetX(), e.GetY(), button);
	pSceneCanvas->SetFocus ();
}
